﻿#region Usings

using System.Windows;
using System.Windows.Interactivity;
using SilverBox.Physics;

#endregion

namespace SilverBox.Behaviors
{
    public abstract class PhysicsBehavior<T> : Behavior<T> where T : FrameworkElement
    {
        private PhysicsContext Context { get; set; }

        protected Simulator Simulator
        {
            get
            {
                if (Context == null)
                {
                    TryGetContext();
                }
                return Context.Simulator;
            }
        }

        private void TryGetContext()
        {
            PhysicsContext context = PhysicsContext.FindContext(AssociatedObject);
            if (context == null)
            {
                context = new PhysicsContext();
                PhysicsContext.SetPhysicsContext(Application.Current.RootVisual, context);
            }

            Context = context;
        }
    }
}